package com.zy.ivideo.service;

import com.zy.ivideo.entity.FocuseEntity;
import com.zy.ivideo.entity.ResponseEntity;
import com.zy.ivideo.repository.FocuseRepository;
import com.zy.ivideo.utils.MD5Util;
import com.zy.ivideo.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLIntegrityConstraintViolationException;

@Service
public class FocuseService {
    @Autowired
    FocuseRepository repository;

    /**
     * 关注
     * @param focuseUserid
     * @param focusedUserid
     * @return
     */
    public ResponseEntity<FocuseEntity> focuse(long focuseUserid,long focusedUserid){
        try{
            FocuseEntity entity=new FocuseEntity();
            entity.setFocuseuserid(focuseUserid);
            entity.setFocuseduserid(focusedUserid);
            entity.setVerifycode(MD5Util.getMD5(String.valueOf(focusedUserid)+String.valueOf(focusedUserid)));
            FocuseEntity result = repository.save(entity);
            if (result!=null){
                return ResponseUtils.success(result);
            }
            return ResponseUtils.failed("关注失败");
        }
        catch (Exception ex){
            return ResponseUtils.failed("关注失败,可能已关注");
        }

    }

    /**
     * 取消关注
     * @param focuseUserid
     * @param focusedUserid
     * @return
     */
    public ResponseEntity<Boolean> unFocuse(long focuseUserid,long focusedUserid){
        try{
            repository.deleteByFocuseuseridAndFocuseduserid(focuseUserid,focusedUserid);
            return ResponseUtils.success(true);
        }
        catch (Exception ex){
            return ResponseUtils.failed("取消关注失败，error:"+ex.getMessage());
        }

    }
}
